package ca.bcit.comp_3512.assignment_02.data_managers;

import java.io.Serializable;
import java.util.List;

import javax.ejb.Stateful;
import javax.enterprise.context.ConversationScoped;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import ca.bcit.comp_3512.assignment_02.data_models.WeeklyEntry;

@ConversationScoped
@Stateful
public class WeeklyEntryManager implements Serializable
{
    @PersistenceContext(unitName="cats_and_more_cats")
    EntityManager em;
    
    public WeeklyEntry find(Integer uniqueID)
    {
        return em.find(WeeklyEntry.class, uniqueID);
    }
    
    public void persist(WeeklyEntry weeklyEntry)
    {
        em.persist(weeklyEntry);
    }
    
    public void merge(WeeklyEntry weeklyEntry)
    {
        em.merge(weeklyEntry);
    }
    
    public void remove(WeeklyEntry weeklyEntry)
    {
        em.remove(find(weeklyEntry.getUniqueID()));
    }
    
    public WeeklyEntry[] getByID(Integer uniqueID)
    {
        TypedQuery<WeeklyEntry> query = em.createQuery("select w from weekly_entries w " + 
                                            " where w.unique_id = " + uniqueID, WeeklyEntry.class);
        List<WeeklyEntry> weeklyEntries = query.getResultList();
        WeeklyEntry[] weeklyEntryArray = new WeeklyEntry[weeklyEntries.size()];
        for (int i = 0; i < weeklyEntryArray.length; ++i)
        {
            weeklyEntryArray[i] = weeklyEntries.get(i);
        }
        return weeklyEntryArray;
    }
    
    public WeeklyEntry[] getAll()
    {
        TypedQuery<WeeklyEntry> query = em.createQuery("select w from weekly_entries w", WeeklyEntry.class);
        List<WeeklyEntry> weeklyEntries = query.getResultList();
        WeeklyEntry[] weeklyEntryArray = new WeeklyEntry[weeklyEntries.size()];
        for (int i = 0; i < weeklyEntryArray.length; ++i)
        {
            weeklyEntryArray[i] = weeklyEntries.get(i);
        }
        return weeklyEntryArray;
    }
}
